/*== TSL_prc_SummaryTimingResultsForReport ===========================================================
* EXAMPLE
EXEC TSL_prc_SummaryTimingResultsForReport 
	@RunId=79, 
	@IncludeTest=1, 
	@IncludeTransaction=1, 
	@IncludePage=1
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_SummaryTimingResultsForReport
		@RunId  int
		,@IncludeTest bit = 1
		,@IncludeTransaction bit = 1
		,@IncludePage bit = 1
	AS
	BEGIN
		---------------------------------------------------------------------------------

		DECLARE @Results TABLE (
			RunId			INT,
			AgentCount		INT NULL,
			MaxUsers		INT NULL,
			
			TotalTests		INT NULL,
			PassedTests		INT NULL,
			FailedTests		INT NULL,
			AvgTT_All		FLOAT NULL,			--Avg Test Time 
			AvgTT_Pass		FLOAT NULL,			--Avg Test Time 
			TestsSec_All	FLOAT NULL,
			TestsSec_Pass	FLOAT NULL,

			TotalTrans		INT NULL,			--Total Trans
			PassTrans		INT NULL,			--Passed Trans
			AvgTrET_All		FLOAT NULL,			--Avg Trans Elapsed Time
			AvgTrET_Pass	FLOAT NULL,			--Avg Trans Elapsed Time
			AvgTrRT_All		FLOAT NULL,			--Avg Trans Response Time
			AvgTrRT_Pass	FLOAT NULL,			--Avg Trans Response Time
			TransSec_All	FLOAT NULL,			--Transactions Per Second - All
			TransSec_Pass	FLOAT NULL,			--Transactions Per Second - Passed

			TotalPages		INT NULL,			--Total Pages
			PassPages		INT NULL,			--Passed Pages All
			PagesSec_All	FLOAT NULL,			--PagesPerSec - All
			PagesSec_Pass	FLOAT NULL,			--PagesPerSec - Passed
			AvgPgT_All		FLOAT NULL,			--Avg Page Time - Passed
			AvgPgT_Pass		FLOAT NULL			--Avg Page Time - All
		)

		---------------------------------------------------------------------------------
		-- Core Summary Info
		---------------------------------------------------------------------------------
		INSERT INTO @Results (RunId)
			SELECT @RunId

		---------------------------------------------------------------------------------
		-- Agent count and max user count
		---------------------------------------------------------------------------------
		UPDATE @Results	SET	AgentCount = (SELECT COUNT(AgentId) FROM LoadTestRunAgent WHERE LoadTestRunId = @RunId)
		UPDATE @Results	SET	MaxUsers = (SELECT TOP 1 ComputedValue FROM LoadTestComputedCounterSample WHERE LoadTestRunId = @RunId 
			AND CategoryName = 'LoadTest:Scenario' AND CounterName = 'User Load' AND InstanceName = '_Total' ORDER BY ComputedValue DESC)

		---------------------------------------------------------------------------------
		-- Test Count and Avg test time
		---------------------------------------------------------------------------------
		IF @IncludeTest = 1
		BEGIN
			;WITH Aggs
			AS	(SELECT	
					COUNT(a.TestDetailId) AS [Total]
					,CAST(CAST(COUNT(a.TestDetailId) AS Decimal) / MIN(b.RunDuration) AS Decimal(15,2)) AS [TestsSec]
					,CAST(AVG(a.ElapsedTime) AS Decimal(15,2)) AS [Response]
				FROM 
					dbo.LoadTestTestDetail AS a
				INNER JOIN 
					LoadTestRun AS b 
					ON a.LoadTestRunId = b.LoadTestRunId
				WHERE 
					a.LoadTestRunId = @RunId 
					AND a.InMeasurementInterval = 1 
					--AND a.Outcome = 10
				)

			UPDATE @Results
			SET
				TotalTests = [Total]
				,TestsSec_All = [TestsSec]
				,AvgTT_All = [Response] 
			FROM Aggs
	
			;WITH Aggs
			AS	(SELECT	
					COUNT(a.TestDetailId) AS [Total]
					,CAST(CAST(COUNT(a.TestDetailId) AS Decimal) / MIN(b.RunDuration) AS Decimal(15,2)) AS [TestsSec]
					,CAST(AVG(a.ElapsedTime) AS Decimal(15,2)) AS [Response]
				FROM 
					dbo.LoadTestTestDetail AS a
				INNER JOIN 
					LoadTestRun AS b 
					ON a.LoadTestRunId = b.LoadTestRunId
				WHERE 
					a.LoadTestRunId = @RunId 
					AND a.InMeasurementInterval = 1 
					AND a.Outcome = 10
				)

			UPDATE @Results
			SET
				PassedTests = [Total]
				,TestsSec_Pass = [TestsSec]
				,AvgTT_Pass = [Response] 
			FROM Aggs
		END

		---------------------------------------------------------------------------------
		-- Transactional Count and Avg trans times
		---------------------------------------------------------------------------------
		IF @IncludeTransaction = 1
		BEGIN
			;WITH Aggs
			AS	(SELECT
				COUNT(DISTINCT (c.TransactionDetailId)) AS [Total]
				,CAST(CAST(COUNT(c.InMeasurementInterval) AS Decimal) / MIN(b.RunDuration) AS Decimal(15,2)) AS [TransSec]
				,CAST(AVG(c.ElapsedTime) AS Decimal(15,2)) AS [Elapsed]
				,CAST(AVG(c.ResponseTime) AS Decimal(15,2)) AS [Response]
			FROM 
				dbo.LoadTestTestDetail AS a
			INNER JOIN 
				LoadTestRun AS b 
				ON a.LoadTestRunId = b.LoadTestRunId
			INNER JOIN dbo.LoadTestTransactionDetail AS c 
				ON a.LoadTestRunId = c.LoadTestRunId 
				AND a.TestDetailId = c.TestDetailId
			WHERE 
				a.InMeasurementInterval = 1 
				AND a.LoadTestRunId = @RunId
				--AND a.Outcome = 10
				)
		
			UPDATE @Results
			SET
				TotalTrans = [Total]
				,TransSec_All = [TransSec]
				,AvgTrET_All = [Elapsed]
				,AvgTrRT_All = [Response] 
			FROM Aggs

			;WITH Aggs
			AS	(SELECT
				COUNT(DISTINCT (c.TransactionDetailId)) AS [Total]
				,CAST(CAST(COUNT(c.InMeasurementInterval) AS Decimal) / MIN(b.RunDuration) AS Decimal(15,2)) AS [TransSec]
				,CAST(AVG(c.ElapsedTime) AS Decimal(15,2)) AS [Elapsed]
				,CAST(AVG(c.ResponseTime) AS Decimal(15,2)) AS [Response]
			FROM 
				dbo.LoadTestTestDetail AS a
			INNER JOIN 
				LoadTestRun AS b 
				ON a.LoadTestRunId = b.LoadTestRunId
			INNER JOIN dbo.LoadTestTransactionDetail AS c 
				ON a.LoadTestRunId = c.LoadTestRunId 
				AND a.TestDetailId = c.TestDetailId
			WHERE 
				a.InMeasurementInterval = 1 
				AND a.LoadTestRunId = @RunId
				AND a.Outcome = 10
				)
		
			UPDATE @Results
			SET
				PassTrans = [Total]
				,TransSec_Pass = [TransSec]
				,AvgTrET_Pass = [Elapsed]
				,AvgTrRT_Pass = [Response] 
			FROM Aggs
		END

		---------------------------------------------------------------------------------
		-- Page Count and Avg Page times
		---------------------------------------------------------------------------------
		IF @IncludePage = 1
		BEGIN
			;WITH Aggs
			AS	(SELECT
					COUNT(DISTINCT (c.PageDetailId)) AS [Total]
					,CAST(CAST(COUNT(c.InMeasurementInterval) AS Decimal) / MIN(b.RunDuration) AS Decimal(15,2)) AS [PagesSec]
					,CAST(AVG(c.ResponseTime) AS Decimal(15,2)) AS [Response]
				FROM 
					dbo.LoadTestTestDetail AS a
				INNER JOIN 
					LoadTestRun AS b 
					ON a.LoadTestRunId = b.LoadTestRunId
				INNER JOIN dbo.LoadTestPageDetail AS c 
					ON a.LoadTestRunId = c.LoadTestRunId 
					AND a.TestDetailId = c.TestDetailId
				WHERE 
					a.InMeasurementInterval = 1 
					AND a.LoadTestRunId = @RunId
					--AND a.Outcome = 10
				)

			UPDATE @Results
			SET
				TotalPages = [Total]
				,PagesSec_All = [PagesSec]
				,AvgPgT_All = [Response] 
			FROM Aggs
		
			;WITH Aggs
			AS	(SELECT
					COUNT(DISTINCT (c.PageDetailId)) AS [Total]
					,CAST(CAST(COUNT(c.InMeasurementInterval) AS Decimal) / MIN(b.RunDuration) AS Decimal(15,2)) AS [PagesSec]
					,CAST(AVG(c.ResponseTime) AS Decimal(15,2)) AS [Response]
				FROM 
					dbo.LoadTestTestDetail AS a
				INNER JOIN 
					LoadTestRun AS b 
					ON a.LoadTestRunId = b.LoadTestRunId
				INNER JOIN dbo.LoadTestPageDetail AS c 
					ON a.LoadTestRunId = c.LoadTestRunId 
					AND a.TestDetailId = c.TestDetailId
				WHERE 
					a.InMeasurementInterval = 1 
					AND a.LoadTestRunId = @RunId
					AND a.Outcome = 10
				)

			UPDATE @Results
			SET
				PassPages = [Total]
				,PagesSec_Pass = [PagesSec]
				,AvgPgT_Pass = [Response] 
			FROM Aggs
		END
		
		---------------------------------------------------------------------------------
		-- Number of Failed tests
		---------------------------------------------------------------------------------
		UPDATE @Results	
		SET FailedTests = TotalTests - PassedTests

		---------------------------------------------------------------------------------
		-- Return the full temp table
		---------------------------------------------------------------------------------
		SELECT * FROM @Results
	END
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_SummaryTimingResultsForReport TO PUBLIC
	GO


/*== TSL_prc_SummaryTimingResultsUsingPerfmon ===========================================================
* EXAMPLE
EXEC TSL_prc_SummaryTimingResultsUsingPerfmon 
	@RunId=79, 
	@IncludeTest=1, 
	@IncludeTransaction=1, 
	@IncludePage=1
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_SummaryTimingResultsUsingPerfmon
		@RunId  int
		,@IncludeTest bit = 1
		,@IncludeTransaction bit = 1
		,@IncludePage bit = 1
	AS
	BEGIN
		SELECT
			LoadTestRunId AS RunId			
			,AgentCount		
			,MaxUsers		
			
			,TotalTests	
			,PassedTests	
			,FailedTests	
			,AvgTT_All		
			,AvgTT_Pass		
			,TestsSec_All	
			,TestsSec_Pass	

			,TotalTrans		
			,TotalTrans AS PassTrans		
			,AvgTrET_All		
			,AvgTrET_Pass	
			,AvgTrRT_All		
			,AvgTrRT_Pass	
			,TransSec_All	
			,TransSec_Pass	

			,TotalPages		
			,PassedPages AS PassPages		
			,PagesSec_All	
			,PagesSec_Pass	
			,AvgPgT_All		
			,AvgPgT_Pass		
		FROM TSL_LoadTestCoreInfo
		WHERE LoadTestRunId = @RunId
	END
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_SummaryTimingResultsUsingPerfmon TO PUBLIC
	GO

/*== TSL_prc_Summary_TestResults =========================================================================
* Gets all of the samples from a given test run for the given prefmon instance for
* use in building a chart
* EXAMPLE:
EXEC	TSL_prc_Summary_TestResults
		@RunId = 9,
		@NameFilter = 'Some Test Name',
		@NumberOfRows = 20

Name			Qty		Min		Avg		Max		StdDev	90%		95%		ScenarioName	TestNameWithId
Immunization	45024	8.01	13.20	116.51	2.82	14.78	15.50	Scenario1		Immunization(000)
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_Summary_TestResults
		@RunId  INT
		,@StartOffset INT = NULL  --Ignored
		,@EndOffset INT = NULL  --Ignored
		,@NameFilter nvarchar(255) = ''
		,@IncludeFailedTests INT = 0  --Ignored
		,@OrderByAverageTime INT = 1  --Ignored
		,@IncludeWarmupCooldown INT = 0  --Ignored
		,@NumberOfRows INT = 25
	AS

--DECLARE @RunId INT = 185
--DECLARE @NameFilter NVARCHAR(2048) = ''
--DECLARE @NumberOfRows INT = 25

	IF CHARINDEX('%', @NameFilter) > 0
	BEGIN
		SELECT TOP (@NumberOfRows)
			b.TestCaseName AS [Name]
			,a.TestsRun AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			,b.ScenarioName
			,b.TestNameWithId
		FROM LoadTestTestSummaryData AS a

		INNER JOIN TSL_View_TestNamesWithIDs AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.TestCaseId = b.TestCaseId

		WHERE 
			a.LoadTestRunId = @RunId
			AND
			b.TestCaseName LIKE @NameFilter
		ORDER BY Average DESC
	END
	ELSE
	BEGIN
		SELECT TOP (@NumberOfRows)
			b.TestCaseName AS [Name]
			,a.TestsRun AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			,b.ScenarioName
			,b.TestNameWithId
		FROM LoadTestTestSummaryData AS a

		INNER JOIN TSL_View_TestNamesWithIDs AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.TestCaseId = b.TestCaseId

		WHERE 
			a.LoadTestRunId = @RunId
		ORDER BY Average DESC
	END		
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_Summary_TestResults TO PUBLIC
	GO


/*== TSL_prc_Summary_TransactionResults ==================================================================
* Gets all of the samples from a given test run for the given prefmon instance for
* use in building a chart
* EXAMPLE:
EXEC	TSL_prc_Summary_TransactionResults
		@RunId = 9,
		@NameFilter = 'Some Transaction Name',
		@NumberOfRows = 20

Id	Name								Qty		Min		Avg		Max		StdDev	90%		95%		ScenarioName	TestCaseName	TransactionNameWithId
38	Immunization.Immunization_CheckIn	45022	0.65	3.48	60.31	1.94	4.89	5.29	Scenario1		Immunization	Immunization.Immunization_CheckIn(038)
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_Summary_TransactionResults
		@RunId  INT
		,@StartOffset INT = NULL  --Ignored
		,@EndOffset INT = NULL  --Ignored
		,@NameFilter nvarchar(255) = ''
		,@IncludeFailedTransactions INT = 0  --Ignored
		,@OrderByAverageTime INT = 1  --Ignored
		,@IncludeWarmupCooldown INT = 0  --Ignored
		,@NumberOfRows INT = 25
	AS

	IF CHARINDEX('%', @NameFilter) > 0
	BEGIN
		SELECT TOP (@NumberOfRows)
			a.TransactionId AS [Id]
			,b.TransactionName AS [Name]
			,a.TransactionCount AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			,b.ScenarioName
			,b.TestCaseName
			,b.TransactionNameWithId
		FROM LoadTestTransactionSummaryData AS a

		INNER JOIN TSL_View_TransactionNamesWithIDs AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.TransactionId = b.TransactionId

		WHERE 
			a.LoadTestRunId = @RunId
			AND
			b.TransactionName LIKE @NameFilter
		ORDER BY Average DESC
	END
	ELSE
	BEGIN
		SELECT TOP (@NumberOfRows)
			a.TransactionId AS [Id]
			,b.TransactionName AS [Name]
			,a.TransactionCount AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			,b.ScenarioName
			,b.TestCaseName
			,b.TransactionNameWithId
		FROM LoadTestTransactionSummaryData AS a

		INNER JOIN TSL_View_TransactionNamesWithIDs AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.TransactionId = b.TransactionId

		WHERE 
			a.LoadTestRunId = @RunId
		ORDER BY Average DESC
	END	
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_Summary_TransactionResults TO PUBLIC
	GO


/*== TSL_prc_Summary_PageResults ==================================================================
* Gets all of the samples from a given test run for the given prefmon instance for
* use in building a chart
* EXAMPLE:
EXEC	TSL_prc_Summary_PageResults
		@RunId = 9,
		@NameFilter = 'Some Page Name',
		@NumberOfRows = 20

Id	Name												Qty		Min		Avg		Max		StdDev	90%		95%
117	IOrderInsuranceService.GetInsuranceEligibility_1SL	45011	0.04	0.87	22.75	0.61	1.43	1.60
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_Summary_PageResults
		@RunId  INT
		,@StartOffset INT = NULL  --Ignored
		,@EndOffset INT = NULL  --Ignored
		,@NameFilter nvarchar(255) = '%'
		,@IncludeFailedPages INT = 0  --Ignored
		,@OrderByAverageTime INT = 1  --Ignored
		,@IncludeWarmupCooldown INT = 0  --Ignored
		,@NumberOfRows INT = 25
	AS

		SELECT TOP (@NumberOfRows)
			a.PageId AS [Id]
			,b.RequestUri AS [Name]
			,a.PageCount AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
		FROM LoadTestPageSummaryData AS a

		INNER JOIN WebLoadTestRequestMap AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.PageId = b.RequestId

		WHERE 
			a.LoadTestRunId = @RunId
			AND
			b.RequestUri LIKE @NameFilter
		ORDER BY Average DESC
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_Summary_PageResults TO PUBLIC
	GO




/*== TSL_prc_Summary_TestResults_New ==================================================================
* Gets all of the samples from a given test run for the given prefmon instance for
* use in building a chart

LoadTestRunId	TestCaseId	PerfmonInstanceId	TestsRun	Average				Minimum		Maximum		Percentile90	Percentile95	Percentile99	Median		StandardDeviation	TestCaseName			ScenarioName	RequestUri
2121			0			10315				45024		13.2010139701937	8.0055549	116.5053613	14.7824601		15.4989695		17.4016468		12.9768325	2.81553830399501	Immunization_Insurance	Scenario1		<blank>
2121			1			10316				11162		12.5344527786508	7.4946873	117.07737	14.1363246		14.9170963		17.2582625		12.2802805	2.41560761404484	Immunization_SelfPay	Scenario1		<blank>
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_Summary_TestResults_New
		@RunId  INT
		,@NameFilter nvarchar(255) = NULL
		,@NumberOfRows INT = 25
	AS

		SELECT TOP (@NumberOfRows)
			a.TestCaseId AS [Id]
			,testcase.TestCaseName AS [Name]
			,TestsRun AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			--,CAST((a.Percentile99) AS Decimal(15,2)) AS [99%]
			--,CAST((a.Median) AS Decimal(15,2)) AS [Med]
			,scenario.ScenarioName
			,a.LoadTestRunId
			,b.PerfmonInstanceId
		FROM LoadTestTestSummaryData AS a
		  
		INNER JOIN TSL_View_PerfmonInstanceIdsToTestIds_Avg AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.TestCaseId = b.TestCaseId
			AND b.InstanceName NOT LIKE '_Agent%'
					  
		LEFT JOIN LoadTestCase As testcase
			ON a.LoadTestRunId = testcase.LoadTestRunId
			AND a.TestCaseId = testcase.TestCaseId
		LEFT JOIN LoadTestScenario As scenario
			ON testcase.LoadTestRunId = scenario.LoadTestRunId
			AND testcase.ScenarioId = scenario.ScenarioId
		
		WHERE 
			a.LoadTestRunId = @RunId
			AND (@NameFilter IS NULL OR testcase.TestCaseName LIKE @NameFilter)
		ORDER BY Average DESC	
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_Summary_TestResults_New TO PUBLIC
	GO


/*== TSL_prc_Summary_TransactionResults_New ==================================================================
* Gets all of the samples from a given test run for the given prefmon instance for
* use in building a chart

LoadTestRunId	TransactionId	PerfmonInstanceId	TransactionCount	Average			Minimum		Maximum		Percentile90	Percentile95	Percentile99	Median	StandardDeviation	TestCaseName						ScenarioName	TransactionName
96				88				4606				32					13.24440625		10.298		19.138		15.723			16.743			19.138			13.411	2.02700403704851	03 - Reprogramacion Financiera		SCRIPTS1-09		03 - 21 - APROBAR CUR
96				47				3859				75					7.5424133333	6.417		9.436		8.208			8.649			9.436			7.496	0.603554142135442	05 - Reporte Dinamico de Gastos_v2	SCRIPTS1-09		05 - 03 - GENERAR REPORTE DINAMICO DE GASTOS
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_Summary_TransactionResults_New
		@RunId  INT
		,@NameFilter nvarchar(255) = NULL
		,@NumberOfRows INT = 25
	AS

		SELECT TOP (@NumberOfRows)
			a.TransactionId AS [Id]
			,trans.TransactionName AS [Name]
			,a.TransactionCount AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			--,CAST((a.Percentile99) AS Decimal(15,2)) AS [99%]
			--,CAST((a.Median) AS Decimal(15,2)) AS [Med]
			,scenario.ScenarioName
			,testcase.TestCaseName
			,a.LoadTestRunId
			,b.PerfmonInstanceId
		  FROM LoadTestTransactionSummaryData AS a

		INNER JOIN TSL_View_PerfmonInstanceIdsToTransactionIds_Avg AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.TransactionId = b.TransactionId
			
			
		LEFT JOIN WebLoadTestTransaction AS trans
			ON trans.LoadTestRunId = a.LoadTestRunId
			AND trans.TransactionId = a.TransactionId
		LEFT JOIN LoadTestCase As testcase
			ON trans.LoadTestRunId = testcase.LoadTestRunId
			AND trans.TestCaseId = testcase.TestCaseId
		LEFT JOIN LoadTestScenario As scenario
			ON testcase.LoadTestRunId = scenario.LoadTestRunId
			AND testcase.ScenarioId = scenario.ScenarioId
		  
		WHERE 
			a.LoadTestRunId = @RunId
			AND (@NameFilter IS NULL OR trans.TransactionName LIKE @NameFilter)
		ORDER BY Average DESC	
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_Summary_TransactionResults_New TO PUBLIC
	GO


/*== TSL_prc_Summary_PageResults_New ==================================================================
* Gets all of the samples from a given test run for the given prefmon instance for
* use in building a chart

LoadTestRunId	PageId	PerfmonInstanceId	PageCount	Average	Minimum	Maximum	Percentile90	Percentile95	Percentile99	Median	StandardDeviation	TestCaseName	ScenarioName	RequestUri
2121			0		10376	22324	0.00841771187959374	0.001	1.58	0.012	0.024	0.151	0.002	0.0402101861239032	Immunization_SelfPay	Scenario1	Signin Page
2121			2		10377	22324	0.00609388998387327	0	0.373	0.013	0.026	0.073	0.001	0.0170050126027342	Immunization_SelfPay	Scenario1	http://stub02/default.aspx
===============================================================================*/
	--START CODE--
	CREATE PROCEDURE TSL_prc_Summary_PageResults_New
		@RunId  INT
		,@NameFilter nvarchar(255) = NULL
		,@NumberOfRows INT = 25
	AS

		SELECT TOP (@NumberOfRows)
			a.PageId AS [Id]
			,request.RequestUri AS [Name]
			,PageCount AS [Qty]
			,CAST((a.Minimum) AS Decimal(15,2)) AS [Min]
			,CAST((a.Average) AS Decimal(15,2)) AS [Avg]
			,CAST((a.Maximum) AS Decimal(15,2)) AS [Max]
			,CAST((a.StandardDeviation) AS Decimal(15,2)) AS [StdDev]
			,CAST((a.Percentile90) AS Decimal(15,2)) AS [90%]
			,CAST((a.Percentile95) AS Decimal(15,2)) AS [95%]
			--,CAST((a.Percentile99) AS Decimal(15,2)) AS [99%]
			--,CAST((a.Median) AS Decimal(15,2)) AS [Med]
			,scenario.ScenarioName
			,testcase.TestCaseName
			,a.LoadTestRunId
			,b.PerfmonInstanceId
		  FROM LoadTestPageSummaryData AS a

		INNER JOIN TSL_View_PerfmonInstanceIdsToPageIds_Avg AS b
			ON a.LoadTestRunId = b.LoadTestRunId
			AND a.PageId = b.PageId
			
			
		LEFT JOIN WebLoadTestRequestMap AS request
			ON request.LoadTestRunId = a.LoadTestRunId
			AND request.RequestId = a.PageId
		LEFT JOIN LoadTestCase As testcase
			ON request.LoadTestRunId = testcase.LoadTestRunId
			AND request.TestCaseId = testcase.TestCaseId
		LEFT JOIN LoadTestScenario As scenario
			ON testcase.LoadTestRunId = scenario.LoadTestRunId
			AND testcase.ScenarioId = scenario.ScenarioId
		  
		WHERE 
			a.LoadTestRunId = @RunId
			AND (@NameFilter IS NULL OR request.RequestUri LIKE @NameFilter)
		ORDER BY Average DESC
	GO
	--START CODE--
	GRANT EXECUTE ON TSL_prc_Summary_PageResults_New TO PUBLIC
	GO

